package com.training.schedule_management_system_admin.service;

import cn.hutool.core.date.DateUtil;
import com.training.schedule_management_system_admin.entity.*;
import com.training.schedule_management_system_admin.entity.VO.ScheduleframeworkVO;
import com.training.schedule_management_system_admin.entity.VO.ScheduletermVO;
import com.training.schedule_management_system_admin.mapper.ScheduleframeworkMapper;
import com.training.schedule_management_system_admin.mapper.ScheduletermMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;


@Service
public class ScheduleframeworkService {

    @Autowired
    ScheduleframeworkMapper scheduleframeworkMapper;

    @Autowired
    ScheduletermMapper scheduletermMapper;

    /*
    得到所有已开放共享的日程框架
     */
    public List<Scheduleframework> getFrameworks() {
        ScheduleframeworkExample example=new ScheduleframeworkExample();
        ScheduleframeworkExample.Criteria criteria = example.createCriteria();
        criteria.andShareStateEqualTo(1);
        return scheduleframeworkMapper.selectByExample(example);
    }

    /*
    获取当前用户下的所有日程框架
     */
    public List<Scheduleframework> getAllWithUser() {
        //获取当前已登录的用户
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User currentUser = (User)session.getAttribute("loginUser");

        ScheduleframeworkExample example=new ScheduleframeworkExample();
        ScheduleframeworkExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(currentUser.getId());
        example.setOrderByClause("publish_state desc,share_state desc");
        return scheduleframeworkMapper.selectByExample(example);
    }

    /*
    根据日程框架id获取日程框架和对应的日程框架项
     */
    public Scheduleframework getFrameworkAndTerm(Integer scheduleframeworkId){
        return scheduleframeworkMapper.getFrameworkAndTermById(scheduleframeworkId);
    }


    /*
    保存日程框架以及对应的日程框架项
     */
    public int saveFrameworkAndTerm(ScheduleframeworkVO sfVO, List<ScheduletermVO> stVOS){
        Scheduleframework scheduleframework=new Scheduleframework();
        //获取当前已登录的用户
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User currentUser = (User)session.getAttribute("loginUser");

        //日程框架
        BeanUtils.copyProperties(sfVO,scheduleframework);//名称和组织
        scheduleframework.setCreateTime(new Date());//创建时间
        scheduleframework.setUserId(currentUser.getId());//创建用户id
        scheduleframework.setShareState(0);//共享状态
        scheduleframework.setStartDate(DateUtil.parse(sfVO.getStartDate(),"yyyy-MM-dd"));//开始日期
        scheduleframework.setEndDate(DateUtil.parse(sfVO.getEndDate(),"yyyy-MM-dd"));//结束日期
        scheduleframework.setDraftState(1);//草稿状态
        scheduleframework.setPublishState(0);//发布状态
        //先插入日程框架
        scheduleframeworkMapper.insertSelective(scheduleframework);

        //日程框架项
        List<Scheduleterm> list=new ArrayList<>();
        for(ScheduletermVO stVO:stVOS){
            Scheduleterm scheduleterm=new Scheduleterm();
            scheduleterm.setScheduleframeworkId(scheduleframework.getId());//设置所关联的日程框架id
            if(!stVO.getOrderNum().equals("") && stVO.getOrderNum()!=null) {
                scheduleterm.setOrderNum(Integer.parseInt(stVO.getOrderNum()));//设置序号
            }
            if(!stVO.getStartTime().equals("") && stVO.getStartTime()!=null ) {
                scheduleterm.setStartTime(DateUtil.parse(stVO.getStartTime(), "HH:mm"));//设置开始时间
            }
            if(!stVO.getEndTime().equals("") && stVO.getEndTime()!=null) {
                scheduleterm.setEndTime(DateUtil.parse(stVO.getEndTime(), "HH:mm"));//设置创建时间
            }
            list.add(scheduleterm);
        }
        //批量插入日程框架项
        return scheduletermMapper.insertList(list);
    }

    /*
    修改日程框架以及对应的日程框架项
     */
    public void updateFrameworkAndTerm(ScheduleframeworkVO sfVO, List<ScheduletermVO> stVOS) {
        Scheduleframework record=new Scheduleframework();
        //日程框架
        BeanUtils.copyProperties(sfVO,record);//名称和组织
        record.setStartDate(DateUtil.parse(sfVO.getStartDate(),"yyyy-MM-dd"));//开始日期
        record.setEndDate(DateUtil.parse(sfVO.getEndDate(),"yyyy-MM-dd"));//结束日期
        //先修改日程框架
        ScheduleframeworkExample example=new ScheduleframeworkExample();
        ScheduleframeworkExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(Integer.parseInt(sfVO.getId()));
        scheduleframeworkMapper.updateByExampleSelective(record,example);

        if(stVOS.get(0).getOrderNum()!=null && !"".equals(stVOS.get(0).getOrderNum())) {//如果日程框架项有修改才调用
            //日程框架项
            List<Scheduleterm> list = new ArrayList<>();
            for (ScheduletermVO stVO : stVOS) {
                Scheduleterm scheduleterm = new Scheduleterm();
                scheduleterm.setScheduleframeworkId(Integer.parseInt(sfVO.getId()));//设置所关联的日程框架id
                scheduleterm.setOrderNum(Integer.parseInt(stVO.getOrderNum()));//设置序号
                if (!stVO.getStartTime().equals("") && stVO.getStartTime() != null) {
                    scheduleterm.setStartTime(DateUtil.parse(stVO.getStartTime(), "HH:mm"));//设置开始时间
                }
                if (!stVO.getEndTime().equals("") && stVO.getEndTime() != null) {
                    scheduleterm.setEndTime(DateUtil.parse(stVO.getEndTime(), "HH:mm"));//设置创建时间
                }
                list.add(scheduleterm);
            }
            //先将该日程框架所对应的日程框架项全部删除
            ScheduletermExample example1 = new ScheduletermExample();
            ScheduletermExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andScheduleframeworkIdEqualTo(Integer.parseInt(sfVO.getId()));
            scheduletermMapper.deleteByExample(example1);
            //再批量插入新的日程框架项
            scheduletermMapper.insertList(list);
        }
    }

    /*
    日程框架发布/关闭、共享/取消共享
     */
    public int state(Scheduleframework framework){
        return scheduleframeworkMapper.updateByPrimaryKeySelective(framework);
    }

    /*
    复制
     */
    public void copy(Scheduleframework framework) {
        Scheduleframework scheduleframework=new Scheduleframework();
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User currentUser = (User)session.getAttribute("loginUser");

        BeanUtils.copyProperties(framework,scheduleframework);
        scheduleframework.setId(null);
        scheduleframework.setName(framework.getName()+"-副本");
        scheduleframework.setCreateTime(new Date());
        scheduleframework.setUserId(currentUser.getId());
        scheduleframework.setDraftState(1);
        scheduleframework.setPublishState(0);
        scheduleframework.setShareState(0);
        scheduleframework.setShareId(null);
        //先插入日程框架
        scheduleframeworkMapper.insertSelective(scheduleframework);

        //日程框架项
        List<Scheduleterm> list=new ArrayList<>();
        for(Scheduleterm scheduleterm:scheduleframework.getScheduleterms()){
            scheduleterm.setId(null);
            scheduleterm.setScheduleframeworkId(scheduleframework.getId());
            list.add(scheduleterm);
        }
        //批量插入日程框架项
        scheduletermMapper.insertList(list);
    }

    /*
    根据共享ID得到日程框架项
     */
    public Scheduleframework getByShareId(String shareId) {
        ScheduleframeworkExample example=new ScheduleframeworkExample();
        ScheduleframeworkExample.Criteria criteria = example.createCriteria();
        criteria.andShareIdEqualTo(shareId);
        List<Scheduleframework> scheduleframeworks = scheduleframeworkMapper.selectByExample(example);
        if(!scheduleframeworks.isEmpty()) {
            return scheduleframeworks.get(0);
        }else{
            return null;
        }
    }

    /*
    模糊查询
     */
    public List<Scheduleframework> findFrameworkLike(String value) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User currentUser = (User)session.getAttribute("loginUser");

        ScheduleframeworkExample example=new ScheduleframeworkExample();
        ScheduleframeworkExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(currentUser.getId());
        criteria.andNameLike("%"+value+"%");
        List<Scheduleframework> frameworks =scheduleframeworkMapper.selectByExample(example);
        if(frameworks!=null && !frameworks.isEmpty()) {
            return frameworks;
        }else{
            return Collections.emptyList();
        }
    }
}
